//
//  FactoryResetView.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/16.
//

import SwiftUI
import TLCLock

struct FactoryResetView: View {


    @State var serverDate: Date = Server.now()

    let device: LockDevice

    var body: some View {
        VStack {
            YxrDatePicker(title: "服务器时间", date: $serverDate)
            
            YxrConfirmView {
                onTapConfirm()
            }
            
            LogView()
        }.onAppear {
            serverDate = Server.now()
        }
        .yxrNavigationTitle("恢复出厂设置")
    }

    func onTapConfirm() {
        guard let lockKey = Server.getSecret(device.macAddress) else {
            showToast("没有lockKey")
            return
        }

        lockTask(desc: "恢复出厂设置") {
            let result = await device.factoryReset(serverDate: serverDate, lockKey: lockKey, trackId: Server.testTrackId)
            if result.isSuccess {
                Server.testLockkey = ""
                Log.info("恢复出厂设置成功，已重置缓存的lockKey")
            }
            
            return result
        }
    }
}
